<?php
gatekeeper ();
$DEFAULT = "";
elgg_make_sticky_form ( 'registerproject' );
$j = 0;

$project_guid = get_input ( 'project_guid', $DEFAULT );
$total_member = get_input ( 'total_member', $DEFAULT );
$group_guid = get_input ( 'group_guid', $DEFAULT );

$waiting = get_input ( 'waiting' );

$group = get_entity($group_guid);
$project = get_entity ( $project_guid );

if($group){
	if(!$group->isMember(elgg_get_logged_in_user_entity())){
		groups_join_group($group, elgg_get_logged_in_user_entity());
	}
}



if ($waiting || $project->wait_project) {
	include 'waitingregister.php';
	
}

if ($project_guid != $DEFAULT) {
	$listproj_url = elgg_get_site_url () . "submana/listpro/$group_guid";
	$is_error = false;
	
	if ($total_member != $DEFAULT) {
		
		$members = array ();
		$j = 0;
		$ar_student_entities = array ();
		for($i = 0; $i < $total_member; $i ++) {
			// add student to project
			$student_id = get_input ( "member" . ($i + 1), $DEFAULT );
			
			if ($student_id != $DEFAULT) {
				if ($i != 0) {
					$student = get_user_by_username ( $student_id );
				} else {
					$student = elgg_get_logged_in_user_entity ();
				}
				if ($student) {
					
					foreach ( $members as $member ) {
						if ($member == $student->username) {
							$is_error = true;
							register_error ( "Đăng kí không thành công. Sinh viên không thể trùng nhau" );
							forward ( REFERER );
							exit ();
						}
					}
					if (! $is_error) {
						array_push ( $members, "$student->username" );
						$j ++;
					}
				} else {
					$is_error = true;
					register_error ( elgg_echo ( 'submana:project:noneuser', array (
							"$student_id" 
					) ) );
					forward ( REFERER );
					exit ();
				}
			} else {
			}
		}
		
		if ($j < $project->min_student) {
			register_error ( elgg_echo ( 'Số lượng thành viên chưa đủ ' . $project->min_student ) );
			$is_error = true;
			forward ( REFERER );
			exit ();
		} else if ($j > $project->max_student) {
			register_error ( elgg_echo ( 'Số lượng thành viên vượt quá số lượng qui định ' . $project->max_student ) );
			$is_error = true;
			forward ( REFERER );
			exit ();
		}
		
		if (! $is_error) {
			// Chua co nhom nao dang ki
			if (! $project->students) {
				$project->students = $members;
				$project->current_group ++;
				if($project->current_group < $project->max_group){
					$project->priority = PRIORITY_MEDIUM;
				} else {
					$project->priority = PRIORITY_LOW;
				}
				$project->save ();
				if($project->auto_approve){
					$vars ['project_guid'] = $project->guid;
					include 'autoapprove.php';
				}
			} else {
				
				// Da co nhom dang ki nhung chua het slot
				$newproject = new ElggObject ();
				$newproject->subtype = "project";
				$newproject->title = $project->title . " " . ($project->current_group);
				$newproject->description = $project->description;
				$newproject->max_group = 1;
				$newproject->current_group = 1;
				$newproject->group_project = $project->group_project;
				$newproject->max_student = $project->max_student;
				$newproject->min_student = $project->min_student;
				$newproject->deadline = $project->deadline;
				$newproject->container_guid = $project->container_guid;
				$newproject->projectlist_guid = $project->projectlist_guid;
				$newproject->access_id = ACCESS_LOGGED_IN;
				$newproject->semester = $project->semester;
				$newproject->academic_year = $project->academic_year;
				$newproject->students = $members;
				$newproject->parent_project = $project->guid;
				$newproject->priority = PRIORITY_LOW;
				$newproject->save();
				
				if($project->auto_approve){
					$vars ['project_guid'] = $newproject->guid;
					include 'autoapprove.php';
				}
				// Save current project
				$project->current_group ++;
				if($project->current_group < $project->max_group){
					$project->priority = PRIORITY_MEDIUM;
				} else {
					$project->priority = PRIORITY_LOW;
				}
				$project->save ();
				
			}
			// Them lop hoc ma sinh vien dang ki vao danh sach lop hoc da dang ki
			foreach ( $members as $student_username ) {
				$student = get_user_by_username ( $student_username );
				if ($student->classes) {
					$student->classes .= ",$project->group_project`$project->semester`$project->academic_year";
				} else {
					$student->classes .= "$project->group_project`$project->semester`$project->academic_year";
				}
				$student->save ();
			}
			
			elgg_clear_sticky_form ( 'registerproject' );
			system_message ( elgg_echo ( 'submana:message:registersuccess' ) );
			forward ( $listproj_url );
		}
	} else {
		register_error ( elgg_echo ( 'submana:project:registermissing' ) );
		forward ( REFERER );
	}
}